
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>异常处理 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="IO编程.html" />
    
    
    <link rel="prev" href="类的特殊方法.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="基础.html">
            
                <a href="基础.html">
            
                    
                    基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="字符串处理.html">
            
                <a href="字符串处理.html">
            
                    
                    字符串处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="数据结构.html">
            
                <a href="数据结构.html">
            
                    
                    数据结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="数据结构高级内容.html">
            
                <a href="数据结构高级内容.html">
            
                    
                    数据结构高级内容
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="函数.html">
            
                <a href="函数.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="函数式编程.html">
            
                <a href="函数式编程.html">
            
                    
                    函数式编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="面向对象编程.html">
            
                <a href="面向对象编程.html">
            
                    
                    面向对象编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="类的特殊方法.html">
            
                <a href="类的特殊方法.html">
            
                    
                    类的特殊方法
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.10" data-path="异常处理.html">
            
                <a href="异常处理.html">
            
                    
                    异常处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="IO编程.html">
            
                <a href="IO编程.html">
            
                    
                    IO编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="模块.html">
            
                <a href="模块.html">
            
                    
                    模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="标准库.html">
            
                <a href="标准库.html">
            
                    
                    标准库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="测试.html">
            
                <a href="测试.html">
            
                    
                    测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="调试.html">
            
                <a href="调试.html">
            
                    
                    调试
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >异常处理</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x5F02;&#x5E38;&#x5904;&#x7406;">&#x5F02;&#x5E38;&#x5904;&#x7406;</h1>
<hr>
<p>Python&#x6240;&#x6709;&#x7684;&#x9519;&#x8BEF;&#x7C7B;&#x578B;&#x90FD;&#x7EE7;&#x627F;&#x81EA; BaseException&#xFF0C;&#x6240;&#x4EE5;&#x5728;&#x4F7F;&#x7528; except &#x65F6;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x7684;&#x662F;&#xFF0C;&#x5B83;&#x4E0D;&#x4F46;&#x6355;&#x83B7;&#x8BE5;&#x7C7B;&#x578B;&#x7684;&#x9519;&#x8BEF;&#xFF0C;&#x8FD8;&#x628A;&#x5176;&#x5B50;&#x7C7B;&#x4E5F;&#x201C;&#x4E00;&#x7F51;&#x6253;&#x5C3D;&#x201D;&#x3002;<br>&#x4E00;&#x4E2A; except &#x8BED;&#x53E5;&#x53EF;&#x4EE5;&#x540C;&#x65F6;&#x5305;&#x62EC;&#x591A;&#x4E2A;&#x5F02;&#x5E38;&#x540D;&#xFF0C;&#x4F46;&#x9700;&#x8981;&#x7528;&#x62EC;&#x53F7;&#x62EC;&#x8D77;&#x6765;&#x3002;&#x4F8B;&#x5982;&#xFF1A; </p>
<pre><code class="lang-python"><span class="hljs-keyword">except</span> (RuntimeError, TypeError, NameError):
    <span class="hljs-keyword">pass</span>
</code></pre>
<p>&#x5982;&#x679C; except &#x540E;&#x9762;&#x6CA1;&#x6709;&#x8DDF;&#x5F02;&#x5E38;&#x540D;&#xFF0C;&#x8868;&#x793A;&#x5B83;&#x5339;&#x914D;&#x4EFB;&#x4F55;&#x7C7B;&#x578B;&#x7684;&#x5F02;&#x5E38;&#xFF0C; <code>except:</code> &#x5FC5;&#x987B;&#x653E;&#x5728;&#x6700;&#x540E;&#x3002;<br>&#x5982;&#x679C;&#x9519;&#x8BEF;&#x6CA1;&#x6709;&#x88AB;&#x6355;&#x83B7;&#xFF0C;&#x5B83;&#x5C31;&#x4F1A;&#x4E00;&#x76F4;&#x5F80;&#x4E0A;&#x629B;&#xFF0C;&#x6700;&#x540E;&#x88AB; Python &#x89E3;&#x91CA;&#x5668;&#x6355;&#x83B7;&#xFF0C;&#x6253;&#x5370;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;&#x4FE1;&#x606F;&#xFF0C;&#x7136;&#x540E;&#x7A0B;&#x5E8F;&#x9000;&#x51FA;&#x3002;<br>&#x4F7F;&#x7528;raise&#x53EF;&#x4EE5;&#x629B;&#x51FA;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;&#xFF0C;raise &#x8BED;&#x53E5;&#x5982;&#x679C;&#x4E0D;&#x5E26;&#x53C2;&#x6570;&#xFF0C;&#x5C31;&#x4F1A;&#x628A;&#x5F53;&#x524D;&#x9519;&#x8BEF;&#x539F;&#x6837;&#x629B;&#x51FA;&#x3002;  </p>
<pre><code class="lang-python"><span class="hljs-keyword">try</span>:
    r = <span class="hljs-number">10</span> / <span class="hljs-number">0</span>
<span class="hljs-keyword">except</span> ZeroDivisionError <span class="hljs-keyword">as</span> e:    <span class="hljs-comment">#e&#x662F;&#x4E00;&#x4E2A;&#x5F02;&#x5E38;&#x7C7B;&#x7684;&#x5B9E;&#x4F8B;&#x53D8;&#x91CF;</span>
    print(<span class="hljs-string">&apos;except:&apos;</span>, e)
    <span class="hljs-keyword">raise</span>              <span class="hljs-comment">#&#x5C06;&#x6355;&#x83B7;&#x7684;&#x9519;&#x8BEF;&#x7EE7;&#x7EED;&#x5F80;&#x4E0A;&#x629B;&#x51FA;&#xFF0C;&#x8BA9;&#x9876;&#x5C42;&#x8C03;&#x7528;&#x8005;&#x53BB;&#x5904;&#x7406;</span>
<span class="hljs-keyword">except</span>:                <span class="hljs-comment">#&#x5176;&#x5B83;&#x5F02;&#x5E38;</span>
    print(<span class="hljs-string">&apos;other error&apos;</span>)
<span class="hljs-keyword">else</span>:                  <span class="hljs-comment">#&#x53EA;&#x5728;&#x6CA1;&#x6709;&#x5F02;&#x5E38;&#x53D1;&#x751F;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#x6267;&#x884C;</span>
    print(<span class="hljs-string">&apos;else...&apos;</span>)   
<span class="hljs-keyword">finally</span>:               <span class="hljs-comment">#&#x4E0D;&#x7BA1;&#x5F02;&#x5E38;&#x53D1;&#x751F;&#x4E0E;&#x5426;&#x90FD;&#x4F1A;&#x6267;&#x884C;</span>
    print(<span class="hljs-string">&apos;finally...&apos;</span>)
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="类的特殊方法.html" class="navigation navigation-prev " aria-label="Previous page: 类的特殊方法">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="IO编程.html" class="navigation navigation-next " aria-label="Next page: IO编程">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"异常处理","level":"1.10","depth":1,"next":{"title":"IO编程","level":"1.11","depth":1,"path":"IO编程.md","ref":"IO编程.md","articles":[]},"previous":{"title":"类的特殊方法","level":"1.9","depth":1,"path":"类的特殊方法.md","ref":"类的特殊方法.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"异常处理.md","mtime":"2017-08-22T15:18:36.908Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2017-08-27T04:11:18.522Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

